package com.xhwl.data.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xhwl.common.dto.cent.sdata.RoomDTO;
import com.xhwl.common.enums.RoomOrientationEnum;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.sdata.Room;
import com.xhwl.common.query.cent.sdata.RoomManageQuery;
import com.xhwl.data.dao.IRoomDao;
import com.xhwl.data.service.IRoomManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author yf
 * @Description:
 * @Date Create in 15:23 2022/06/15
 */
@Service
public class RoomManageServiceImpl implements IRoomManageService {
    @Autowired
    private IRoomDao roomDao;

    @Override
    public ResultJson update(RoomDTO roomDTO) {
        Integer update = roomDao.update(null, new UpdateWrapper<Room>()
                .set("manage_type_id", roomDTO.getManageTypeId())
                .eq("id", roomDTO.getId()));
        return update == 1 ? ResultJson.success("操作成功") : ResultJson.fail("操作失败");
    }

    @Override
    public Map<String, Object> page(RoomManageQuery roomManageQuery) {
        Integer total = roomDao.manageCount(roomManageQuery);
        if (total == 0) {
            return null;
        } else {
            if (null == roomManageQuery.getSize()) {
                roomManageQuery.setSize(10);
            }
            if (null == roomManageQuery.getPage()) {
                roomManageQuery.setPage(0);
            } else {
                Integer page = (roomManageQuery.getPage() - 1) * roomManageQuery.getSize();
                roomManageQuery.setPage(page);
            }
            List<RoomDTO> roomDTOS = roomDao.managePage(roomManageQuery);
            List<RoomDTO> records = roomDTOS.stream().map(x -> {
                RoomOrientationEnum orientation = x.getOrientation();
                x.setOrientationName(orientation.getDescription());
                return x;
            }).collect(Collectors.toList());
            Map<String, Object> map = new HashMap<>(2);
            map.put("total", total);
            map.put("records", records);
            return map;
        }
    }
}
